home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_571 / memclear / memclear.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  104 lines

  1. #include <exec/exec.h>
  2. #include <exec/execbase.h>
  3.  
  4. /* MemClear.c - (c) 1986-90 Dallas John Hodgson VER 1.05
  5.  
  6.     = Compiled under Aztec C : 32-bit int mode =
  7.  
  8.        MemClear walks through the free memory lists zeroing along the way.
  9. Written mainly as an exercise, but may prove useful to some. This kind of
  10. system manipulation should never be performed in interrupt code; see RKM:Exec,
  11. Pg. 56 for more information.
  12.  
  13.     Memory management consists of a linked list of MemHeaders, each
  14. of which points to a linked list of MemChunks. The address of the first
  15. MemHeader is contained in ExecBase, yielding access to the whole mess.
  16.  
  17.     Each MemHeader oversees a hardware RAM partition, such as CHIP
  18. memory or a contiguous block of FAST RAM. It contains information regarding
  19. Attribute, (CHIP|FAST|PUBLIC) the block's address space, free space remaining,
  20. free list pointer, and link. The free bytes count is the sum total of all
  21. the Length fields in its free list; a linked list of MemChunks each containing
  22. a Length field and a link.
  23.  
  24.     Note that there are no actual pointers to the free space itself;
  25. Rather, the MemChunks are like Exec Messages in that there is a link node
  26. on top followed by 0..N bytes of free space. The MemChunk Length fields
  27. include the sizeof() the MemChunk structure itself, so a MemChunk cannot be
  28. shorter than 2 longwords. What AllocMem() does is searches each MemHeader that
  29. matches your attribute and looks for the FIRST MemChunk that fits. The node is
  30. unlinked from the chain, and its address returned. If the allocation was
  31. smaller than the node, the excess space is again partitioned up and linked
  32. into the MemChunk free list. The Length field of the MemHeader is decreased
  33. by the size of your allocation. Keep in mind that all allocations are rounded
  34. up to the nearest 8 bytes. Also, MemChunks are coalesced together whenever
  35. their address spaces are contiguous; it is imperative that the largest
  36. contiguous space possible be maintained for large-chunk applications such as
  37. bitmap allocation.
  38.  
  39. This new version allows the user to specify the memory fill byte (0 by
  40. default) & display additional diagnostic information regarding CHIP and
  41. FAST RAM fragmentation. */
  42.  
  43. extern struct ExecBase *SysBase;
  44.  
  45. main(argc,argv)
  46. int argc;
  47. char *argv[];
  48. {
  49.   struct MemHeader *memhdr;
  50.   struct MemChunk *memchunk;
  51.   ULONG fast_total=0,chip_total=0;
  52.   UWORD fast_count=0,chip_count=0,flag;
  53.   unsigned value=0;
  54.  
  55.   printf("MemClear v1.05 : © 1986-90, Dallas J. Hodgson\n");
  56.  
  57.   if (argc>1) {
  58.     if (!sscanf(argv[1],"%x",&value)) {
  59.       puts("Usage : MemClear [hex byte-value]");
  60.       exit(100);
  61.     }
  62.     if (value>0xff) {
  63.       puts("Value must be within range of 00-FF");
  64.       exit(100);
  65.     }
  66.   }
  67.   
  68.   Forbid(); /* task switching OFF */
  69.  
  70.   /* traverse linked list of MemHeaders */
  71.  
  72.   for (memhdr=(struct MemHeader *)SysBase->MemList.lh_Head;
  73.           memhdr->mh_Node.ln_Succ;
  74.               memhdr=(struct MemHeader *)memhdr->mh_Node.ln_Succ) {
  75.  
  76.     flag=memhdr->mh_Attributes & MEMF_CHIP;
  77.  
  78.     /* traverse linked list of MemChunks */
  79.  
  80.     for (memchunk=memhdr->mh_First;memchunk;memchunk=memchunk->mc_Next) {
  81.  
  82.       /* zap free space; but don't touch the node! */
  83.  
  84.       if (memchunk->mc_Bytes>sizeof(*memchunk))
  85.         setmem((char *)memchunk+sizeof(*memchunk),
  86.           memchunk->mc_Bytes-sizeof(*memchunk),value);
  87.  
  88.       if (flag) {
  89.         chip_total+=memchunk->mc_Bytes-sizeof(*memchunk);
  90.         chip_count++;
  91.       }
  92.       else {
  93.         fast_total+=memchunk->mc_Bytes-sizeof(*memchunk);
  94.         fast_count++;
  95.       }
  96.     }  
  97.   }
  98.  
  99.   Permit();
  100.  
  101.   printf("%7d bytes of CHIP memory in %d MemChunks filled w/value %xH.\n",chip_total,chip_count,value);
  102.   printf("%7d bytes of FAST memory in %d MemChunks filled w/value %xH.\n",fast_total,fast_count,value);
  103. }
  104.